package gemini.pollux.ui.client.page.content.product.producttype.edit;

import gemini.basic.model.ProductType;
import gemini.pollux.ui.client.ServiceCatalog;
import gemini.pollux.ui.client.mvp.callback.AbstractAsyncCallback;
import gemini.pollux.ui.client.mvp.single.PolluxWidgetDisplay;
import gemini.pollux.ui.client.mvp.single.PolluxWidgetPresenter;
import gemini.pollux.ui.client.page.RootEvent;
import gemini.pollux.ui.client.page.content.product.producttype.ProductTypeEvent;
import gemini.pollux.ui.client.utils.GuiUtils;
import gemini.pollux.ui.client.utils.UIConstants;
import ch.elca.gwt.mvp.client.EventBus;
import ch.elca.gwt.mvp.client.place.Place;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.inject.Inject;

public class ProductTypeEditPresenter extends
		PolluxWidgetPresenter<ProductTypeEditPresenter.Display> {

	@Inject
	public ProductTypeEditPresenter(Display display, EventBus globalEventBus) {
		super(display, globalEventBus);
	}

	public interface Display extends PolluxWidgetDisplay {
		HasClickHandlers getBackClickHandlers();

		HasClickHandlers getSubmitClickHandlers();

		ProductType getProductType();

		void setProductType(ProductType productType);

	}

	public static final Place PLACE = new Place(UIConstants.PLACE_PRODUCT_TYPE);

	@Override
	protected void onBind() {
		registerHandler(display.getBackClickHandlers().addClickHandler(
				new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						localEventBus.fireEvent(new ProductTypeEvent(
								ProductTypeEvent.ForwardType.BACK_TO_LIST));
					}
				}));

		registerHandler(display.getSubmitClickHandlers().addClickHandler(
				new ClickHandler() {

					@Override
					public void onClick(ClickEvent event) {
						eventBus.fireEvent(new RootEvent(
								RootEvent.ForwardType.MASK));
						ServiceCatalog.getProductTypeService().saveOrUpdateProductType(
								display.getProductType(),
								new AbstractAsyncCallback<ProductType>() {

									@Override
									public void onSuccess(ProductType result) {
										eventBus.fireEvent(new RootEvent(
												RootEvent.ForwardType.UN_MASK));
										productType = result;
										confirmBackToList();
									}
								});
					}
				}));

		display.setProductType(productType);
		display.initCustom();
	}
	
	private void confirmBackToList() {
		String message = "Save or update product type successfull!";

		GuiUtils.info(message);

		localEventBus.fireEvent(new ProductTypeEvent(
				ProductTypeEvent.ForwardType.BACK_TO_LIST));
	}

	@Override
	public Place getPlace() {
		return PLACE;
	}

	private ProductType productType;

	@Override
	public void setInputObject(Object ob) {
		this.productType = (ProductType) ob;
	}

	@Override
	public void onUnbind() {
		productType = null;
	}
}
